<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        {
            let p1 = new Promise(resolve=>{
                setTimeout(()=>{
                    resolve(1111);
                },1000);
            });
            let p2 = new Promise(resolve=>{
                setTimeout(()=>{
                    resolve(2222);
                },3000);
            });
            let p3 = new Promise(resolve=>{
                setTimeout(()=>{
                    resolve(3333);
                },2000);
            });
            function myProAll(arr){
                return new Promise(resolve=>{
                    let count = 0 ; 
                    let newArr = [] ;
                    arr.forEach(item=>{
                        item.then(res=>{
                            newArr.push(res);
                            count ++ ;
                            if( count == arr.length ){
                                resolve(newArr);
                            }
                        })
                    });
                });
            }
            myProAll([p1,p2,p3]).then(res=>{
                console.log(res);
            });
        }
        {
            function myAllSettled(arr){
                return new Promise(resolve=>{
                    let count = 0 ; 
                    let newArr = new Array(arr.length);
                    arr.forEach(item=>{
                        item.then(res=>{
                            let obj = {} ;
                            obj['PromiseState'] = "fulfilled";
                            obj['PromiseResult'] = res;
                            newArr[count] = obj ;
                            count ++ ;
                            if( count == arr.length ){
                                resolve(newArr);
                            }
                        },rej=>{
                            let obj = {} ;
                            obj['PromiseState'] = "rejected";
                            obj['reason'] = rej ;
                            newArr[count] = obj ;
                            count ++ ;
                            if( count == arr.length ){
                                resolve(newArr);
                            }
                        });
                    });
                });
            }    
            let p1 = new Promise(resolve=>{
                setTimeout(()=>{
                    resolve(1111);
                },1000);
            });
            let p2 = new Promise((resolve,reject)=>{
                setTimeout(()=>{
                    reject(2222);
                },3000);
            });
            let p3 = new Promise(resolve=>{
                setTimeout(()=>{
                    resolve(3333);
                },2000);
            });
            myAllSettled([p1,p2,p3]).then(res=>{
                console.log(res);
            });        
        }
    </script>
</body>
</html>